package com.prs.crm.dao.hr.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.hr.PayrollBillDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.hr.Payroll;
import com.prs.crm.domain.hr.PayrollBill;
import com.prs.crm.domain.hr.Person;

public class PayrollBillDaoImpl extends BaseDaoImpl<PayrollBill> implements PayrollBillDao{

	@Override
	public Collection<Integer> findPersonIds(Payroll payroll) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.createAlias("person", "p");
		criteria.setProjection(Projections.distinct(Projections.property("p.id")));
		return criteria.list();
	}

	@Override
	public List<PayrollBill> getCurrentPayrollBill(Person person,
			Payroll payroll) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("person", person));
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.addOrder(Order.desc("type"));
		return criteria.list();
	}

	@Override
	public String getPersonPayrollState(Payroll payroll, Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.add(Restrictions.eq("person", person));
		criteria.setProjection(Projections.distinct(Projections.property("state")));
		return (String)criteria.list().get(0);
	}

	@Override
	public List<PayrollBill> getIncreaseItem(Payroll payroll) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.add(Restrictions.eq("type", "增项"));
		return criteria.list();
	}

	@Override
	public List<PayrollBill> getReduceItem(Payroll payroll) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.add(Restrictions.eq("type", "减项"));
		return criteria.list();
	}

	@Override
	public List<PayrollBill> getIncreaseItem(Payroll payroll, Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.add(Restrictions.eq("person", person));
		criteria.add(Restrictions.eq("type", "增项"));
		return criteria.list();
	}

	@Override
	public List<PayrollBill> getReduceItem(Payroll payroll, Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.add(Restrictions.eq("person", person));
		criteria.add(Restrictions.eq("type", "减项"));
		return criteria.list();
	}

	@Override
	public Collection<PayrollBill> getAllPayrollBill() {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		return criteria.list();
	}

	@Override
	public Collection<?> getReportDataSource(Collection<Person> persons, Payroll payroll) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.add(Restrictions.in("person", persons));
		criteria.add(Restrictions.eq("type", "增项"));
		ProjectionList projectionlist = Projections.projectionList();  
    	projectionlist.add(Projections.groupProperty("person"));
    	projectionlist.add(Projections.sum("value"));
    	criteria.setProjection(projectionlist);
		return criteria.list();
	}

	@Override
	public Collection<?> findAllPayrollBill(Collection<Person> persons,
			Payroll payroll) {
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("payroll", payroll));
		criteria.add(Restrictions.in("person", persons));
		return criteria.list();
	}

	@Override
	public Collection<Integer> getPayrollIdsByPerson(Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(PayrollBill.class);
		criteria.add(Restrictions.eq("person", person));
		criteria.add(Restrictions.eq("state", Constrains.PAYROLL_ISSUE));
		criteria.createAlias("payroll", "p");
		criteria.setProjection(Projections.distinct(Projections.property("p.id")));
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<PayrollBill> getPayrollBillByPayroll(final Payroll payroll) {
		// TODO Auto-generated method stub
		return (Collection<PayrollBill>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(PayrollBill.class);
				criteria.add(Restrictions.eq("payroll", payroll));
				return criteria.list();
			}
			
		});
	}

}
